package com.meitianhui.convenitenbuy.module.home.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.home.model.bean.HomeGoodsInfo;
import com.meitianhui.convenitenbuy.util.StringUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/11/3.
 * 淘淘领
 */
public class HomeEdinburghGoodsAdapter extends BaseQuickAdapter<HomeGoodsInfo.Convient, BaseViewHolder> {
    private Context context;
    private RequestOptions requestOptions = null;
    public HomeEdinburghGoodsAdapter(Context context, List data) {
        super(R.layout.item_home_edinburgh_goods_item, data);
        this.context = context;
}

    @Override
    protected void convert(BaseViewHolder helper, HomeGoodsInfo.Convient item) {
        TextView tvType = helper.getView(R.id.tv_type);
        TextView tvTaoBao = helper.getView(R.id.tv_taobao_price);
        tvTaoBao.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        ImageView ivStorePic = helper.getView(R.id.iv_store_pic);
        ImageView ivStoreSellOut = helper.getView(R.id.iv_store_sell_out);
        helper.setText(R.id.tv_hand_price, "¥" + item.getDiscount_price());   //到手的价格
        helper.setText(R.id.tv_title, "\u3000\u3000\u3000" + item.getTitle());  //标题
        String taoBaoPrice = item.getTaobao_price();   //淘宝价格

//        if(TextUtils.isEmpty(item.getTaobao_link())&&TextUtils.isEmpty(item.getTaobao_sales())){
//            tvType.setBackgroundResource(R.drawable.edinburgh_self_text_bg);
//            tvType.setTextColor(mContext.getResources().getColor(R.color.homeEdinburghSelfText));
//            tvType.setText("自营");
//        }else{
//
//        }
        if(TextUtils.equals(item.getData_source(),"meitianhui")){
            tvType.setBackgroundResource(R.drawable.edinburgh_self_text_bg);
            tvType.setTextColor(mContext.getResources().getColor(R.color.homeEdinburghSelfText));
            tvType.setText("自营");
            tvTaoBao.setText("原价" + item.getMarket_price());
            String money = StringUtils.moneySub(item.getMarket_price(), item.getDiscount_price());
            helper.setText(R.id.tv_save_price, "节省¥" + money);
        }else if(TextUtils.equals(item.getData_source(),"huiguo")){
            tvType.setBackgroundResource(R.drawable.will_self_text_bg);
            tvType.setTextColor(mContext.getResources().getColor(R.color.homeWillText));
            tvType.setText("会过");
            tvTaoBao.setText("原价" + item.getMarket_price());
            String money = StringUtils.moneySub(item.getMarket_price(), item.getDiscount_price());
            helper.setText(R.id.tv_save_price, "节省¥" + money);
        }else{
            if (TextUtils.isEmpty(item.getTaobao_link())) {
                tvType.setBackgroundResource(R.drawable.edinburgh_text_bg);
                tvType.setTextColor(mContext.getResources().getColor(R.color.homeEdinburghText));
                tvType.setText("淘宝");
            } else {
                tvType.setBackgroundResource(R.drawable.edinburgh_text_blue_bg);
                tvType.setTextColor(mContext.getResources().getColor(R.color.speedText));
                tvType.setText("极速领");
            }
            if (TextUtils.isEmpty(taoBaoPrice)) {
                tvTaoBao.setText("淘宝价¥" + item.getMarket_price());
                String money = StringUtils.moneySub(item.getMarket_price(), item.getDiscount_price());
                helper.setText(R.id.tv_save_price, "节省¥" + money);
            } else {
                tvTaoBao.setText("淘宝价¥" + taoBaoPrice);
                String money = StringUtils.moneySub(taoBaoPrice, item.getDiscount_price());
                helper.setText(R.id.tv_save_price, "节省¥" + money);
            }
        }

        try {
            int saleQty = Integer.parseInt(item.getSale_qty());
            String taoBaoSales = item.getTaobao_sales();
            if (TextUtils.isEmpty(taoBaoSales)) {
                helper.setText(R.id.iv_sell_count, 0 + "");
            } else {
                helper.setText(R.id.iv_sell_count, taoBaoSales + "");
            }
            if (saleQty <= 0) {
                ivStoreSellOut.setVisibility(View.VISIBLE);
            } else {
                ivStoreSellOut.setVisibility(View.GONE);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        if (requestOptions == null) {
            requestOptions = new RequestOptions().dontAnimate()
                    .override(250, 250)
                    .centerCrop()
                    .skipMemoryCache(false)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .placeholder(R.mipmap.icon_default_picture_store)
                    .error(R.mipmap.icon_default_picture_store);

        }
        Glide.with(context).load(item.getPic_info_url()).apply(requestOptions).thumbnail(0.1f).into(ivStorePic);


    }

}
